
/*
  sandbox_webga: Sandbox overrides for GrADS.

  Author: Daniel da Silva <ddasilva@umd.edu>
 */

#include <execinfo.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int trace_has_func(char *func_name) {
  void *array[10];
  size_t size;
  char **strings;
  size_t i, j;
  int found = 0;
  
  size = backtrace(array, 10);
  strings = backtrace_symbols(array, size);

  for(i=0; i < size; i++) {    
    char *s = strings[i];
    int m = -1, n = -1;

    for (j=0; j < strlen(s); j++) {
      if (s[j] == '(') {
	m = j + 1;
      } else if (m > 0 && s[j] == '+') {
	n = j;
      }
    }
    s[n] = '\0';
    s = s+m;
    //printf("%s\n", s);
    if (!strcmp(s, func_name)) {
      found = 1;
      break;
    }
  }
  free(strings);
  return found;
}


int system(const char *command) {
  printf("System calls not supported in gaweb.\n");
}

size_t fwrite(const void *array, size_t size, size_t count, FILE *stream) {
  if (trace_has_func("png_write_data") || trace_has_func("readline")) {
    return fwrite_unlocked(array, size, count, stream);
  } else {
    printf("Fwrite calls not supported in gaweb.\n");
  }
}

int fputc(int c, FILE *fp) {
  printf("Fputc calls not supported in gaweb.\n");
}

int fputs(const char *str, FILE *stream) {
  printf("Fputs calls not supported in gaweb.\n");
}
